//
//  DetailNameViewController.m
//  Beauty
//
//  Created by Snow on 16/9/8.
//  Copyright © 2016年 Snow. All rights reserved.
//

#import "DetailNameViewController.h"
#import "GXMacro.h"
@interface DetailNameViewController ()<UITextFieldDelegate>
@property (nonatomic, strong) UITextField *text;
@end

@implementation DetailNameViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"用户昵称";
    self.text = [[UITextField alloc] initWithFrame:CGRectMake(0, 10, SCREEN_WIDTH, 50)];
    [self.view addSubview:_text];
    self.text.text = self.placeHolder;
    self.text.borderStyle = UITextBorderStyleLine;
    self.text.clearButtonMode = UITextFieldViewModeAlways;
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(handleSave:)];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor grayColor];
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    if (![textField.text isEqualToString:self.placeHolder]) {
        self.navigationItem.rightBarButtonItem.tintColor = [UIColor redColor];
    } else {
        self.navigationItem.rightBarButtonItem.tintColor = [UIColor greenColor];
    }
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    if (![textField.text isEqualToString:self.placeHolder]) {
        self.navigationItem.rightBarButtonItem.tintColor = [UIColor redColor];
        
    } else {
        self.navigationItem.rightBarButtonItem.tintColor = [UIColor greenColor];
    }
}
- (void)handleSave:(UIBarButtonItem *)barButtonItem
{
    if (![_text.text isEqualToString:self.placeHolder]) {
        barButtonItem.tintColor = [UIColor lightGrayColor];
        if ([self.delegate respondsToSelector:@selector(sendTextName:)] && self.delegate != nil) {
            [self.delegate sendTextName:_text.text];
        }
        [self.navigationController popViewControllerAnimated:YES];
        
    } else {
    barButtonItem.tintColor = [UIColor grayColor];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
